package com.gm.baseplatform.base.bootstrap;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Boot extends HttpServlet {

	private static final long serialVersionUID = -5447643954355216933L;
	
	private Log log = LogFactory.getLog(Boot.class); 

	public static interface BootFlag {
		public void boot(Boot boot) throws BootException;
		public String getBootRoot();
	}
	
	public static final String BOOT_ROOT_SPRING = "spring";
	public static final String BOOT_ROOT_SERVLET = "servlet";
	
	public static List<Boot.BootFlag> bootList = new LinkedList<Boot.BootFlag>();
	static {
		bootList.add(new ConfigurationBoot());
		bootList.add(new ModuleBoot());
		bootList.add(new ResourceCacheBoot());
	}
	
	public void bootInSpring() {
		log.info("Boot In Spring Start Running......");
		for (int i = 0,j = bootList.size(); i < j; i++) {
			BootFlag bootFlag = bootList.get(i);
			if (bootFlag == null || !(bootFlag.getBootRoot().equals(BOOT_ROOT_SPRING))) {
				continue;
			}
			try {
			   bootFlag.boot(this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void init() throws ServletException  { 
		 super.init();
		 bootInServlet();
	}
	
	public void bootInServlet() {
		log.info("Boot In Servlet Start Running......");
		for (int i = 0,j = bootList.size(); i < j; i++) {
			BootFlag bootFlag = bootList.get(i);
			if (bootFlag == null || !(bootFlag.getBootRoot().equals(BOOT_ROOT_SERVLET))) {
				continue;
			}
			try {
			   bootFlag.boot(this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
